package com.cabrida.livestar.main;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.FansVLogData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.SearchData;
import com.cabrida.livestar.star.StarActivity;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 搜索
 */
public class SearchActivity extends BaseActivity {
	public static final String TAG = SearchActivity.class.getSimpleName();
	private EditText et_des;
	private TextView tv_cancel;
	private ListView listView;
	private SearchAdapter adapter;
	private List<SearchData> data_list=new ArrayList<SearchData>();
	private BroadcastReceiver mReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(MainActivity.ACTION_VLOG_ATTENTION_LIST)) {
				String s_udid  = intent.getStringExtra("s_udid");
				getAttentionBtn(s_udid);
			}
		}
	};
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	};
	
	/** 点击关注数据 */
	private void getAttentionBtn(final String s_udid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("follow", s_udid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_VLOG_ATTENTION_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAttentionBtn", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							String status=JsonUtils.getString(data,"status", "");
							String number=JsonUtils.getString(data,"number", "");
							for (int i = 0; i < data_list.size(); i++) {
								if (s_udid.equals(data_list.get(i).getSuid().toString())) {
									data_list.get(i).setS_status(status);
									data_list.get(i).setS_booked(number);
								}
							}
							adapter.refreshData(data_list);
							adapter.notifyDataSetChanged();
							showToast(message);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	
	private void getSearchData(String content) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("content", content);

		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_SEARCH_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getSearchData", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);

						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							data_list = JSONParser.getFansSearchList(list);
							adapter.refreshData(data_list);
							adapter.notifyDataSetChanged();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message = JsonUtils.getString(data, "message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_search);
		et_des = (EditText) findViewById(R.id.et_des);
		tv_cancel = (TextView) findViewById(R.id.tv_cancel);
		listView = (ListView) findViewById(R.id.listView);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		adapter = new SearchAdapter(context);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				FansVLogData data = (FansVLogData) arg0.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				intent.putExtra("suid", data.getS_udid().toString());
				intent.setClass(context, StarActivity.class);
				context.startActivity(intent);
			}
		});
		
		tv_cancel.setOnClickListener(this);
		et_des.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(arg0)) {
					getSearchData(arg0.toString());
				}
			}
		});
	
		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(MainActivity.ACTION_VLOG_ATTENTION_LIST);
		registerReceiver(mReceiver, filter_ad);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.tv_cancel:
			this.finish();
			break;
		default:
			break;
		}
	}

}
